package in.smartride.matchingengine;

import com.google.appengine.api.users.UserServiceFactory;

import in.smartride.dataInterface.userdata.*;
import in.smartride.dataInterface.DataHandler;

import java.util.Date;
import java.util.Calendar;
import java.util.TimeZone;

public class FilterPreferences {
	DataHandler dh;
	
	public FilterPreferences() {
		dh = new DataHandler();
	}

	public boolean IsCompatible(String CheckUserId) {
		//CheckUserId is one of the results for findride/offerride for CurrentUserId
		//We just check their compatibility here.
		String CurrentUserId = UserServiceFactory.getUserService().getCurrentUser().getEmail();
		UserProfile CheckUserProf = dh.getUserProfile(CheckUserId);
		UserProfile CurrentUserProf = dh.getUserProfile(CurrentUserId);
		UserPreferences CheckUserPref = dh.getUserPreferences(CheckUserId);
		UserPreferences CurrentUserPref = dh.getUserPreferences(CurrentUserId);
		return Check(CheckUserPref, CurrentUserProf) && Check(CurrentUserPref, CheckUserProf);
	}
	
	private boolean Check(UserPreferences UserPref, UserProfile UserProf) {
		System.out.println("Gender === " + UserPref.getGender());
		if((UserPref.getGender()==1 && UserProf.getGender()==false) ||
		   (UserPref.getGender()==2 && UserProf.getGender()==true))
			return false;
		//get the servers GMT Time
		Date date = new Date();
		TimeZone tzGMT = TimeZone.getTimeZone("GMT");
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.setTimeZone(tzGMT);
		
		cal.add(Calendar.YEAR, -UserPref.getLowerAge());
		Date lower = cal.getTime();
		cal.add(Calendar.YEAR, UserPref.getLowerAge()-UserPref.getUpperAge());
		Date upper = cal.getTime();

		if(UserProf.getDateOfBirth().before(lower) || UserProf.getDateOfBirth().after(upper))
			return false;
		return true;
	}
}
